import * as Spec from 'pactum/src/models/Spec'

describe('Auth登录认证 e2e测试', () => {
  let spec: Spec;
  beforeEach(() => {
    spec = global.spec as Spec;
  })
  // 注册用户
  it('注册用户', async () => {
    const user = { username: 'test123', password: '123456' }
    return spec.post('/auth/signup')
      .withBody(user)
      .expectStatus(201)
      .expectBodyContains(user.username)
      .expectJsonLike({
        username: user.username,
        roles: [
          {id: 2, name: '普通用户'}
        ]
      })
  })
  // 注册新用户
  // 重复注册该用户
  it('重复注册该用户', async () => {
    const user = { username: 'test123', password: '123456' }

    await global.pactum.spec().post('/auth/signup').withBody(user);

    return spec.post('/auth/signup')
      .withBody(user)
      .expectStatus(403)
      .expectBodyContains('用户已存在')
  })
  // 注册用户传参异常 username password -> 长度，类型，空
  it('注册用户传参异常 username 长度', async () => {
    const user = { username: 'a', password: '123456' }

    return spec.post('/auth/signup')
      .withBody(user)
      .expectStatus(400)
      .expectBodyContains('用户名长度必须在4到20个字符之间');
  })

  // 登录用户
  it('登录用户', async () => {
    const user = { username: 'test123', password: '123456' }

    await global.pactum.spec().post('/auth/signup').withBody(user);

    return spec.post('/auth/signin')
      .withBody(user)
      .expectStatus(201)
      .expectBodyContains('access_token')
  })
  // 登录用户传参异常 username password -> 长度，类型，空
  it('登录用户传参异常 username 长度', async () => {
    const user = { username: 'test123', password: '123456' }

    await global.pactum.spec().post('/auth/signup').withBody(user);

    return spec.post('/auth/signin')
      .withBody({username: 'a'})
      .expectStatus(400)
      .expectBodyContains('用户名长度必须在4到20个字符之间')
  })
  // 登录用户不存在
  it('登录用户不存在', async () => {
    const user = { username: 'test123', password: '123456' }

    return spec.post('/auth/signin')
      .withBody(user)
      .expectStatus(403)
      .expectBodyContains('用户不存在，请注册')
  })
  // 登录用户密码错误
  it('登录用户密码错误', async () => {
    const user = { username: 'test123', password: '123456' }

    await global.pactum.spec().post('/auth/signup').withBody(user);

    return spec.post('/auth/signin')
      .withBody({...user, password: '1234567'})
      .expectStatus(403)
      .expectBodyContains('用户名或密码错误')
  })
})
